package com.qfedu.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.AdinmEntity;
import com.qfedu.entity.RolesEntity;
import com.qfedu.mapper.AdminMapper;
import com.qfedu.service.AdminService;
import com.qfedu.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class AdminServiceImp implements AdminService {

    @Autowired(required = false)
    AdminMapper adminMapper;

    //注册
    @Override
    public boolean insertuser(String username, String password) {
        return adminMapper.insertuser(username, password);
    }

    @Override
    public AdinmEntity queryuser(String username, String password) {
        return adminMapper.queryuser(username,password);
    }

    @Override
    public AdinmEntity queryuserbyname(String username) {
        return adminMapper.queryuserbyname(username);
    }

    //查询角色
    @Override
    public Set<String> queryRoleByusername(String username) {
        return adminMapper.queryRoleByusername(username);
    }

    //查询超级管理员
    @Override
    public Page mapperList(AdinmEntity ad) {
        Page page = new Page();
        page.setPageNum(ad.getPageNum());
        page.setPageSize(ad.getPageSize());
        int count = adminMapper.count();
        page.setCount(adminMapper.count());
        System.out.println(count);

        List<AdinmEntity> list = adminMapper.mapperList(page.getStart(), page.getPageSize());
        page.setAdinmEntities(list);
        return page;
    }

    @Override
    public Page mapperListrole(RolesEntity ro) {
        Page page1 = new Page();
        page1.setPageNum(ro.getPageNum());
        page1.setPageSize(ro.getPageSize());
        page1.setCount(adminMapper.countrole());

        List<RolesEntity> roles = adminMapper.bianroles(page1.getStart(), page1.getPageSize());
        page1.setRolesEntities(roles);
        return page1;
    }

    @Override
    public AdinmEntity adminList(AdinmEntity ad) {
        return adminMapper.adminlist(ad);
    }

    @Override
    public boolean adminedit(AdinmEntity ad) {
        return adminMapper.adminedit(ad);
    }

    //查询所有角色
    @Override
    public List<RolesEntity> queryroles() {
        return adminMapper.queryroles();
    }

    //删除管理员
    @Override
    public boolean deletebyid(int aid) {
        return adminMapper.delebyid(aid);
    }

    //启用管理员
    @Override
    public boolean qiyongbyid(int aid) {
        return adminMapper.qiyongbyid(aid);
    }

    @Override
    public boolean deAll(String ids) {
        String str = "("+ids.substring(0,ids.length()-1)+")";

        return adminMapper.deAll(str);
    }

    //管理员提交
    @Override
    public boolean tiaoadmin(AdinmEntity ad) {
        return adminMapper.tiaoadmin(ad);
    }

    //角色查询
    @Override
    public RolesEntity selrole(RolesEntity ro) {
        return adminMapper.selrole(ro);
    }

    //角色修改提交
    @Override
    public boolean editrole(RolesEntity ro) {
        return adminMapper.editrole(ro);
    }

    //角色添加提交
    @Override
    public boolean addroleti(RolesEntity ro) {
        return adminMapper.addroleti(ro);
    }

    //删除角色
    @Override
    public boolean deleterole(int rid) {
        boolean dele = adminMapper.dele(rid);
        boolean ii = adminMapper.deleterole(rid);
        if (ii){
            return true;

        }else {
            return false;
        }

    }

    //用户角色编辑解绑重绑
    @Override
    public boolean sendadmin(AdinmEntity ad) {

        boolean adminedit = adminMapper.adminedit(ad);
        if (adminedit){
            boolean jiebang = adminMapper.jiebang(ad.getAid());

                for (int i=0;i<ad.getRoles().length;i++){
                    boolean insertrole = adminMapper.insertrole(ad.getAid(), ad.getRoles()[i]);
                }

            return true;
        }else {
            return false;
        }

    }
}
